package com.sobot.finance.poc.web.controller;

import com.google.gson.Gson;
import com.sobot.finance.poc.web.config.TaskConfig;
import com.sobot.finance.poc.web.config.SysConfig;
import com.sobot.finance.poc.web.controller.response.GeneralRes;
import com.sobot.finance.poc.web.util.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author baiyp
 */
@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {

    @Resource
    private RedisClient redisClient;

    @Resource
    private SysConfig sysConfig;

    @Resource
    private TaskConfig taskConfig;

    @RequestMapping("/redis/set/{key}/{val}")
    public GeneralRes set(@PathVariable("key") String key, @PathVariable("val") String val) {
        log.info("[设置缓存-开始]: {}-{}", key, val);
        redisClient.set(key, val);
        log.info("[设置缓存-结束]");
        return GeneralRes.success();
    }

    @RequestMapping("/redis/get/{key}")
    public GeneralRes get(@PathVariable("key") String key) {
        GeneralRes res = GeneralRes.success();
        log.info("[查询缓存-开始]: {}", key);
        String val = redisClient.get(key);
        res.setData(val);
        log.info("[查询缓存-结束]: {}", val);
        return res;
    }

    @RequestMapping("/conf")
    public GeneralRes conf() {
        log.info("[查询配置-开始]");
        Gson gson = new Gson();
        log.info("[配置]: {}", gson.toJson(sysConfig.getLimit()));
        log.info("[配置]: {}", gson.toJson(sysConfig.getRetryFlag()));
        log.info("[配置]: {}", gson.toJson(taskConfig.getCron()));
        log.info("[配置]: {}", gson.toJson(taskConfig.getRetry()));
        log.info("[查询配置-结束]");
        return GeneralRes.success();
    }
}
